<title>机构管理</title>

<div class="layui-fluid">
  <div class="layui-card">
    <form class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="LAY_audit_form">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">审核状态</label>
          <div class="layui-input-block">
            <select name="auditStatus">
              <option value="">请选择</option>
              <option value="-1">无需审核</option>
              <option value="0">待审核</option>
              <option value="1">审核失败</option>
              <option value="2">待认证</option>
              <option value="3">认证成功</option>
              <option value="4">认证失败</option>
            </select>
          </div>
        </div>

        <div class="layui-inline">
          <label class="layui-form-label">机构名称</label>
          <div class="layui-input-block">
            <input type="text" name="orgName" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>

        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY_audit_form_search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 查询
          </button>
          <button type="reset" class="layui-btn layuiadmin-btn-useradmin">
            <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i> 重置
          </button>
        </div>
      </div>
    </form>
    

    <div class="layui-card-body">
        <table id="LAY_audit" lay-filter="LAY_audit"></table>
          
        <script type="text/html" id="LAY_audit_toolbar" lay-filter="LAY_audit_toolbar">
          {{# if (d.auditStatus == 0) { }}
          <a class="layui-btn layui-btn-xs" lay-event="auditResolve">审核通过</a>
          <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="auditRejected">审核驳回</a>
          {{# } }}
          {{# if (d.auditStatus == 2) { }}
          <a class="layui-btn layui-btn-xs" lay-event="attestationResolve">认证通过</a>
          <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="attestationRejected">认证失败</a>
          {{# } }}
        </script>
    </div>
  </div>
</div>

<script type="text/html" id="LAY_attestation_modal">
  <div class="layui-fluid">
    <form class="layui-form" lay-filter="LAY_attestation_modal">
      <div class="layui-form-item">
        <label class="layui-form-label">备案状态</label>
        <div class="layui-input-block">
          <select name="putOnRecordStatus" lay-verify="required">
            <option value="">请选择</option>
            <option value="1">已备案</option>
            <option value="2">无需备案</option>
          </select>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">协议状态</label>
        <div class="layui-input-block">
          <select name="protocolStatus" lay-verify="required">
            <option value="">请选择</option>
            <option value="1">协议签署已完成</option>
          </select>
        </div>
      </div>
      <p>说明：</p>
      <p>认证通过后，渠道可以拥有渠道管理权限，并可以与我司进行推广费结算</p>
      <button class="LAY_attestation_modal_save" style="display: none;" lay-submit lay-filter="LAY_attestation_modal_save">保存</button>
    </form>
  </div>
</script>

<script type="text/html" id="LAY_audit_modal">
  <div class="layui-fluid">
    <form class="layui-form" lay-filter="LAY_audit_modal">
      <div class="layui-form-item">
        <label class="layui-form-label">业务归属</label>
        <div class="layui-input-block">
          <input type="text" name="businessBelongTo" lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
        </div>
      </div>
      <p>说明：</p>
      <p>审核通过后，渠道账户即可登录惠易保进行业务推广，但没有渠道管理权限，也不会真正结算推广费</p>
      <button class="LAY_audit_modal_save" style="display: none;" lay-submit lay-filter="LAY_audit_modal_save">保存</button>
    </form>
  </div>
</script>

<script>
  layui.use(['admin', 'form', 'element', 'laypage', 'laytpl', 'table'], function () {
    var $ = layui.$,
    admin = layui.admin,
    setter = layui.setter,
    view = layui.view,
    table = layui.table,
    laytpl = layui.laytpl,
    laypage = layui.laypage,
    element = layui.element,
    form = layui.form,
    params = {};

    
    initForm();
    initAudit();

    function initForm() {
      // 初始化表单
      form.render(null, 'LAY_audit_form');

      // 表单提交
      form.on('submit(LAY_audit_form_search)', function (data) {
        var field = data.field;
        params = $.extend(true, params, field);

        reloadAudit(params, 1);
        return false;
      });
    }

    // 初始化table
    function initAudit() {
      var field = form.val('LAY_audit_form');
      params = $.extend(true, params, field);

      table.render({
        elem: '#LAY_audit'
        ,where: params
        ,page: true
        ,url: setter.url + '/org/channelAuditPageList' //模拟接口
        ,data: []
        ,cols: [[
          { field: 'serialNumber', align: 'center', title: '审核流水号' }
          ,{ field: 'applyTime', align: 'center', title: '审核申请时间' }
          ,{ field: 'orgName', align: 'center', title: '机构名称' }
          ,{ field: 'parentOrgSimpleName', align: 'center', title: '上级机构名称' }
          ,{ field: 'orgCategoryDesc', align: 'center', title: '机构类型' }
          ,{ field: 'auditStatusDesc', align: 'center', title: '审核状态' }
          ,{ field: 'businessBelongTo', align: 'center', title: '业务归属' }
          ,{ field: 'putOnRecordStatusDesc', align: 'center', title: '备案状态' }
          ,{ field: 'protocolStatusDesc', align: 'center', title: '协议状态' }
          ,{ title: '操作', width: 300, align: 'center', fixed: 'right',  toolbar: '#LAY_audit_toolbar' }
        ]]
        ,parseData: function(res){
          return {
            "code": res.code,
            "msg": res.msg,
            "data": res.data.list,
            "count": res.data.pageInfo.count,
          };
        }
        ,response: {
          statusCode: 200 //规定成功的状态码，默认：0
        } 
        ,request: {
          limitName: 'pageSize'
        }
      });

      table.on('tool(LAY_audit)', function(obj){
        var data = obj.data;

        switch (obj.event) {
          case 'auditResolve':
            auditResolve(data.channelAuditId);
            break;
          case 'auditRejected':
            auditRejected(data.channelAuditId);
            break;
          case 'attestationResolve':
            attestationResolve(data.channelAuditId);
            break;
          case 'attestationRejected':
            attestationRejected(data.channelAuditId);
            break;
            
          default:
            break;
        }
      });
    }

    function auditResolve(channelAuditId) {
      var html = $('#LAY_audit_modal').html();

      layer.open({
        type: 1,
        title: '审核通过',
        area: ['600px'],
        content: html,
        btn: ['确定', '取消'],
        success: function (layero, index) {
          form.render(null, 'LAY_audit_modal');
          form.on('submit(LAY_audit_modal_save)', function (data) {
            var field = data.field;
            updataAudit($.extend(true, {}, field, {
              channelAuditId: channelAuditId,
              result: true
            }), function () {
              layer.close(index);
            });
            return false;
          });
        },
        yes: function(index, layero){
          $('.LAY_audit_modal_save').click();
        }
      });
    }

    function auditRejected(channelAuditId) {
      layer.confirm('您确定审核不通过吗？不通过后，该渠道将直接删除', {
        title: '审核驳回',
        yes: function (index, layero) {
          updataAudit({
            channelAuditId: channelAuditId,
            result: false
          }, function () {
            layer.close(index);
          });
        }
      });
    }

    function attestationResolve(channelAuditId) {
      var html = $('#LAY_attestation_modal').html();

      layer.open({
        type: 1,
        title: '认证通过',
        area: ['600px'],
        content: html,
        btn: ['确定', '取消'],
        success: function (layero, index) {
          form.render(null, 'LAY_attestation_modal');
          form.on('submit(LAY_attestation_modal_save)', function (data) {
            var field = data.field;
            updataAttestation($.extend(true, {}, field, {
              channelAuditId: channelAuditId,
              result: true
            }), function () {
              layer.close(index);
            });
            return false;
          });
        },
        yes: function(index, layero){
          $('.LAY_attestation_modal_save').click();
        }
      });
    }

    function attestationRejected(channelAuditId) {
      layer.confirm('您确定认证失败吗？失败后渠道将直接禁用，已有业务请线下与渠道联系解决', {
        title: '认证失败',
        yes: function (index, layero) {
          updataAttestation({
            channelAuditId: channelAuditId,
            result: false
          }, function () {
            layer.close(index);
          });
        }
      });
    }

    function updataAudit(data, callback) {
      admin.req({
        url: setter.url + '/org/channelExamine',
        type: 'POST',
        data: data,
        done: function (res) {
          var data = res.data;
          var msg = '操作失败';
          var icon = 2;

          if (data) {
            msg = '操作成功';
            icon = 1;
          }
          
          layer.msg(msg, {
            icon: icon,
            time: 1500,
            shade: [0.3, '#393D49']
          });

          reloadAudit(params);

          if (typeof(callback) == 'function') {
            callback();
          }
        }
      });
    }

    function updataAttestation(data, callback) {
      admin.req({
        url: setter.url + '/org/channelAuth',
        type: 'POST',
        data: data,
        done: function (res) {
          var data = res.data;
          var msg = '操作失败';
          var icon = 2;

          if (data) {
            msg = '操作成功';
            icon = 1;
          }
          
          layer.msg(msg, {
            icon: icon,
            time: 1500,
            shade: [0.3, '#393D49']
          });

          reloadAudit(params);

          if (typeof(callback) == 'function') {
            callback();
          }
        }
      });
    }

    // reload table
    function reloadAudit(params, curr, callback) {
      table.reload('LAY_audit', {
        where: params
        ,page: {
          curr: curr
        }
        ,done: function (res) {
          if (typeof(callback) == 'function') {
            callback(res);
          }
        }
      })
    }

  });
</script>